package com.woniuxy.microfixeddepositserver.controller;

import com.alibaba.fastjson.JSONObject;
import com.woniuxy.bean.BillBean;
import com.woniuxy.bean.ResultBean;
import com.woniuxy.bean.regularbean.MemberAccountInnerBean;
import com.woniuxy.microfixeddepositserver.bean.FixedTimeDepositBean;
import com.woniuxy.microfixeddepositserver.bean.ProductDateInfo;
import com.woniuxy.microfixeddepositserver.bean.detail.OutterRegularBean;
import com.woniuxy.microfixeddepositserver.bean.detail.OwnRegularBean;
import com.woniuxy.microfixeddepositserver.bean.detail.RegularQuery;
import com.woniuxy.microfixeddepositserver.dao.FixedTimeDepositDao;
import com.woniuxy.microfixeddepositserver.dao.RegularDao;
import com.woniuxy.microfixeddepositserver.service.IRegularService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.joda.time.DateTime;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

/**
 * @author: masterhai
 * @date: 2021/4/12:9:58
 * @description: 定期产品操作控制类
 */
@RefreshScope
@Api("定期理财产品前端app专用接口")
@RestController
@Slf4j
@RequestMapping(value = "/regular", produces = {"application/json;charset=utf-8"})
public class RegularOperationController {
    @Resource
    private IRegularService regularService;

    @ApiOperation("购买定期理财产品")
    @PostMapping("buy")
    public ResultBean buyRegular (@ApiParam("购买对象") @RequestBody BillBean billBean) {
        if (ObjectUtils.isEmpty(billBean)) {
            return ResultBean.ParamError();
        }
        //购买
        return regularService.buyRegular(billBean);
    }


    /**
     * 根据id查询理财产品
     */
    @ApiOperation("根据id查询定期理财产品,前端app专用")
    @PostMapping("findProductDeatils")
    public ResultBean selectByPrimaryKey (@ApiParam("查询对象") @RequestBody RegularQuery regularQuery) {
        if (ObjectUtils.isEmpty(regularQuery)) {
            return ResultBean.ParamError();
        }
        return regularService.getProductDeatil(regularQuery);
    }

    //出售定期产品
    @ApiOperation("出售定期理财产品")
    @PostMapping("sell")
    public ResultBean sellRegular (@ApiParam("购买对象") @RequestBody BillBean billBean) {
        if (ObjectUtils.isEmpty(billBean)) {
            return ResultBean.ParamError();
        }
        if (org.springframework.util.StringUtils.isEmpty(billBean.getProductName())) {
            //未传产品名字,做赎回费用计算
            return regularService.getMoney(billBean.getPid(), billBean.getProductId());
        }
        //出售
        billBean.setPayType("余额");
        return regularService.sellRegular(billBean);
    }

    //查询用户购买的定期产品
    @ApiOperation("app专用查询用户购买的定期理财产品列表")
    @PostMapping("findBuy")
    public ResultBean findBuyRegular (@ApiParam("查询对象") @RequestBody BillBean billBean) {
        if (ObjectUtils.isEmpty(billBean)) {
            return ResultBean.ParamError();
        }
        //出售
        return regularService.findBuyRegular(billBean.getMemberName());
    }

    @ApiOperation("门户端调用查询用户购买的定期收益")
    @PostMapping("getMemberEarning")
    public ResultBean getMemberEarning (@RequestParam(value = "username") String username) {
        if (StringUtils.isEmpty(username)) {
            return ResultBean.ParamError();
        }
        //查询收益
        return regularService.getRegularEarnings(username);
    }

    @ApiOperation("获取假设当天购买产品相关收益计算日期信息")
    @PostMapping("getEarningsDayInfo")
    public ResultBean getEarningsDayInfo (@ApiParam("购买对象") @RequestBody RegularQuery regularQuery) {
        if (ObjectUtils.isEmpty(regularQuery)) {
            return ResultBean.ParamError();
        }
        ProductDateInfo productDateInfo = regularService.computeDate(null, regularQuery.getFid());
        if (ObjectUtils.isEmpty(productDateInfo)) {
            return ResultBean.builder().code(557).msg("获取产品收益计算日期信息失败").build();
        }
        //查询收益
        return ResultBean.builder().code(200).msg("获取产品收益计算信息成功").data(productDateInfo).build();
    }


//    @ApiOperation("测试定时任务更新所有用户购买定期的收益,调用一次收益更新一天")
//    @PostMapping("testTimer")
//    public ResultBean testTimerTask () {
//        //确定年份天数
//        Integer days = 365;
//        Integer year = Integer.valueOf(new DateTime().toString("yyyy"));
//        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {//闰年的判断规则
//            days = 366;
//        }
//        //获取当前用户购买的所有产品信息
//        List<MemberAccountInnerBean> regularsList = fixedTimeDepositDao.getRegulars();
//        Random random = new Random();
//        for (int i = 0; i < regularsList.size(); i++) {
//            MemberAccountInnerBean bean = regularsList.get(i);
//            //判断当天是否属于收益计算日
//            Integer todayTime = Integer.valueOf(new DateTime().toString("yyyyMMdd"));
//            if (todayTime.equals(bean.getCalculateEarningsDay()) && bean.getConfirmCapital() - 0 == 0) {
//                //将未生效金额移入已生效金额
//                bean.setConfirmCapital(bean.getUnConfirmCapital());
//                //未生效金额重置为0
//                bean.setUnConfirmCapital(0.0);
//            }
//            //获取确认计息的本金
//            Double confirmCapital = bean.getConfirmCapital();
//            //年利率
//            Double yieldRates = bean.getYieldRates();
//            //获取日利率
//            double dayRates = yieldRates / days;
//            //获取当天倍率
//            double rate = (random.nextInt(100) + 50) / 100d;
//            //收益
//            double todayEarnings = confirmCapital * dayRates * rate;
//            //获取平台的收益
//            Integer deduct = fixedTimeDepositDao.getPlatformDeduct(bean.getFixedDepositId());
//            if (deduct > 0 && deduct <= 100 && todayEarnings > 0) {
//                Double trusteeFee = todayEarnings * deduct / 100;
//                todayEarnings -= trusteeFee;
//                //储存平台收益
//                regularDao.saveTrusteeFee("定期", trusteeFee);
//            }
//            //封装数据,当前产品当日收益
//            bean.setTodayEarnings(todayEarnings);
//            //总收益
//            Double totalEarnings = bean.getTotalEarnings() + todayEarnings;
//            bean.setTotalEarnings(totalEarnings);
//            //产品总价值(生效本金+未生效本金+总收益收益)
//            bean.setOverAllBalance(bean.getConfirmCapital() + bean.getUnConfirmCapital() + totalEarnings);
//        }
//        //更新收益
//        fixedTimeDepositDao.updateEarnings(regularsList);
//
//        return ResultBean.builder().code(200).msg("更新用户定期资产中心数据成功").build();
//    }
}
